﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;

public class CardManager
{
    public BaseEntity _BaseEntity;
    public BaseEntityData entityData;
    
    public Dictionary<DeckType, List<GameObject>> cardInstances = new Dictionary<DeckType, List<GameObject>>();
 
    public Dictionary<DeckType, List<GameObject>> currentDeckInstances = new Dictionary<DeckType, List<GameObject>>();
 
    public Dictionary<DeckType, List<GameObject>> remainDeckInstances = new Dictionary<DeckType, List<GameObject>>();
    public Dictionary<DeckType, List<GameObject>> recycleDeckInstances = new Dictionary<DeckType, List<GameObject>>();

    public CardManager(BaseEntity baseEntity)
    {
        this._BaseEntity = baseEntity;
    }
    
    public void InitialDeck(BaseEntityData data, bool isNeedResetDefaultDeck = false)
    {
        this.entityData = data;

        InitialDeckInstanceContainer();

        if (isNeedResetDefaultDeck)
        {
            SetDefaultDeck();
        }
        
        InstantiateCardDeck();
    }

    public void RecycleCard(DeckType deckType)
    {
        while (recycleDeckInstances[deckType].Count > 0)
        {
            remainDeckInstances[deckType].Add(recycleDeckInstances[deckType].Pop());
        }
    }
    
    public void DiscardCard(GameObject cardInstance, DeckType deckType){
        currentDeckInstances[deckType].Remove(cardInstance);
        recycleDeckInstances[deckType].Add(cardInstance);
    }
    
    public void RecycleCardOfFixed()
    {
        RecycleCard(DeckType.Fixed);
    }

    public void RecycleCardOfAction()
    {
        RecycleCard(DeckType.Action);
    }
    
    public void SetDefaultDeck()
    {
        entityData.currentDeck = new List<CardPair>(entityData.defaultDeck);
    }
    
    public bool isActionCardInHoldingDeck()
    {
        return currentDeckInstances[DeckType.Action].Count > 0;
    }

    public bool isFixedCardInHoldingDeck()
    {
        return currentDeckInstances[DeckType.Fixed].Count > 0;
    }
    
    public void InstantiateCardDeck()
    {
        foreach (CardPair deckPair in entityData.currentDeck)
        {
            cardInstances[deckPair.cardDeckType].Add(CardUtils.GetCardInstance(deckPair, _BaseEntity, deckPair.belongSlot, deckPair.cardBelongType, deckPair.cardDeckType));
        }
    }

//    public GameObject GetCardInstance(CardScriptable cardData, int belongSlot = 0, CardBelongType cardBelongType = CardBelongType.Basic, DeckType deckBelongType = DeckType.Action)
//    {
//        switch (cardBelongType)
//        {
//            case CardBelongType.Basic:
//            case CardBelongType.Drag:
//            case CardBelongType.Icon:
//                break;
//            default:
//                throw new ArgumentOutOfRangeException();
//        }
//        
//        GameObject cardInstance = Object.Instantiate(ResourceLoader.cardBelongPrefabMap[cardBelongType]);
//        CardScriptable currentCardData = Object.Instantiate(cardData);
//
//        cardInstance.GetComponent<CardDisplayerMonobehaviour>().card = currentCardData;
//        cardInstance.GetComponent<CardDisplayerMonobehaviour>().SetCard(currentCardData);
//
//        currentCardData.belongSlot = belongSlot;
//        
//        BasicCard cardInfo = cardInstance.GetComponent<BasicCard>(); 
//        
//        cardInfo.belongDeckType = deckBelongType;
//        cardInfo.cardData = currentCardData;
//        cardInfo.caller = _BaseEntity;
//
//        cardInstance.transform.localScale = Vector3.zero;
//
//        return cardInstance;
//    }

    public void ExecuteCurrentCard(DeckType deckType)
    {
        try
        {
            GameObject cardInstance = currentDeckInstances[deckType][0];

            if (cardInstance.GetComponent<BasicCard>().ExecuteCard())
            {
                DiscardCard(cardInstance, deckType);   
            }
        }
        catch (Exception e)
        {
            Debug.Log($"{nameof(GameObject)} 无法使用当前的卡");
        }
    }
    
    public bool PickCardFromRemainDeck(DeckType deckType)
    {
        Debug.Log("<Color=#ff0000>从抽卡区抽卡</Color>");

        bool isCanDrawCard;

        if (remainDeckInstances[deckType].Count > 0)
        {
            Debug.Log("从抽卡区有卡牌剩余，可以抽卡");
            isCanDrawCard = true;
        }
        else
        {
            Debug.Log("从抽卡区没有卡牌剩余");
            isCanDrawCard = false;
        }

        if (isCanDrawCard)
        {
            Debug.Log("可以进行抽卡");
            GameObject card = remainDeckInstances[deckType].Pop();
            currentDeckInstances[deckType].Add(card);
            Debug.Log($"从持卡区抽取一张 {deckType} 卡，当前手牌区的卡牌为：{currentDeckInstances[deckType].Count}, 持卡区的剩余卡牌数为: {remainDeckInstances[deckType].Count}");
        }
        else
        {
            Debug.Log("抽卡区没有卡牌剩余，无法继续抽卡");
        }
        
        return isCanDrawCard;
    }
    
    private void InitialDeckInstanceContainer()
    {
        cardInstances[DeckType.Action] = new List<GameObject>();
        cardInstances[DeckType.Fixed] = new List<GameObject>();

        currentDeckInstances[DeckType.Action] = new List<GameObject>();
        currentDeckInstances[DeckType.Fixed] = new List<GameObject>();

        remainDeckInstances[DeckType.Action] = new List<GameObject>();
        remainDeckInstances[DeckType.Fixed] = new List<GameObject>();

        recycleDeckInstances[DeckType.Action] = new List<GameObject>();
        recycleDeckInstances[DeckType.Fixed] = new List<GameObject>();
    }
    
    public void DiscardCardInstance(GameObject cardInstance, DeckType belongDeckType)
    {
        DiscardCard(cardInstance, belongDeckType);
    }
    
    public void CopyCardInstanceFromDeck()
    {
        remainDeckInstances = new Dictionary<DeckType, List<GameObject>>(cardInstances);
    }

    /// <summary>
    /// 打乱持卡区的卡牌顺序
    /// </summary> 
    /// 
    public void ShufflerRemainDeck(DeckType deckType)
    {
        remainDeckInstances[deckType].Shuffle();
    }

    public void BattleStart()
    {
        Debug.Log("战斗开始");
        // 将 所有卡牌复制一份到当前的 remain 区
        CopyCardInstanceFromDeck();
        
        // 然后打乱 remain 区 Action 类
        ShufflerRemainDeck(DeckType.Action);

        // 已经可以开始回合了
    }
}
